package org.gwh.airagknowledge.api;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.gwh.airagknowledge.core.document.DocumentLoader;
import org.gwh.airagknowledge.entity.Document;
import org.gwh.airagknowledge.repository.DocumentRepository;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/documents")
@RequiredArgsConstructor
@Tag(name = "Document Management", description = "APIs for managing documents")
public class DocumentController {

    private final DocumentRepository documentRepository;
    private final DocumentLoader documentLoader;

    @GetMapping
    @Operation(summary = "Get all documents", description = "Retrieve a list of all documents")
    public ResponseEntity<List<Document>> getAllDocuments() {
        return ResponseEntity.ok(documentRepository.findAll());
    }

    @GetMapping("/knowledge-base/{knowledgeBaseId}")
    @Operation(summary = "Get documents by knowledge base", description = "Retrieve all documents for a specific knowledge base")
    public ResponseEntity<List<Document>> getDocumentsByKnowledgeBase(@PathVariable Long knowledgeBaseId) {
        return ResponseEntity.ok(documentRepository.findByKnowledgeBaseId(knowledgeBaseId));
    }

    @GetMapping("/{id}")
    @Operation(summary = "Get document by ID", description = "Retrieve a single document by its ID")
    public ResponseEntity<Document> getDocumentById(@PathVariable Long id) {
        return documentRepository.findById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @PostMapping("/upload")
    @Operation(summary = "Upload document", description = "Upload a document to a specific knowledge base")
    public ResponseEntity<Document> uploadDocument(
            @RequestParam("file") MultipartFile file,
            @RequestParam("knowledgeBaseId") Long knowledgeBaseId) {
        try {
            Document document = documentLoader.uploadDocument(file, knowledgeBaseId);
            return ResponseEntity.status(HttpStatus.CREATED).body(document);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().build();
        } catch (IOException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "Delete document", description = "Delete a document by its ID")
    public ResponseEntity<Void> deleteDocument(@PathVariable Long id) {
        if (!documentRepository.existsById(id)) {
            return ResponseEntity.notFound().build();
        }
        
        documentRepository.deleteById(id);
        return ResponseEntity.noContent().build();
    }
}